package com.huawei.esdk.tp.device.smc;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.authorize.itf.IAuthorizePolicy;
import com.huawei.esdk.platform.common.MessageContext;
import com.huawei.esdk.platform.common.ThreadLocalHolder;
import com.huawei.esdk.platform.common.bean.aa.AccountInfo;
import com.huawei.esdk.platform.commu.itf.ICXFSOAPProtocolAdapter;
import com.huawei.esdk.tp.common.constants.InfoKeys;
import com.huawei.esdk.tp.common.constants.TPConstant;
import com.huawei.esdk.tp.device.aa.SMC20V1R3C10AAService;
import com.huawei.esdk.tp.device.smc.SMCConnection;
import com.huawei.esdk.tp.device.smc.SMCDevice;
import com.huawei.esdk.tp.southcommu.cxf.gen.AuthenticateResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.LoginRequestResponseSouth;

/**
 * 设备连接类
 * <p>
 * @author  cWX191990
 * @see  [相关类/方法]
 * @since  [eSDK TP V100R003C20]
 */
public class SMC20V1R3C10Connection extends SMCConnection
{
    private static final Logger LOGGER = Logger.getLogger(SMC20V1R3C10Connection.class);
    
    private SMC20V1R3C10AAService smcLoginProcess;
    
    public SMC20V1R3C10Connection(ICXFSOAPProtocolAdapter cxfSOAPProtocolAdapter, SMCDevice serviceProxy, String user,
        String pwd, IAuthorizePolicy authorizePolicy)
    {
        super(cxfSOAPProtocolAdapter, serviceProxy, user, pwd, authorizePolicy);
        smcLoginProcess = new SMC20V1R3C10AAService(cxfSOAPProtocolAdapter);
    }
    
    @Override
    public boolean initConn(String connId)
    {
        MessageContext mc = ThreadLocalHolder.get();
        AccountInfo devAcctInfo = getDevAcctInfo(getAuthorizePolicy());
        if (null == devAcctInfo)
        {
            LOGGER.error("Cannot derive account from the input user " + getLoginUser());
            return false;
        }
        String userName = devAcctInfo.getUserId();
        String pwd = devAcctInfo.getPassword();
        
        prepareSessionId(connId);
        AuthenticateResponseSouth authRes;
        if (isLocalAuth())
        {
            LoginRequestResponseSouth loginRes = smcLoginProcess.smcLoginRequest(userName);
            if (null == loginRes)
            {
                return false;
            }
            //DTS2015011901913 修改
            else if (0 != loginRes.getLoginRequestResult().getErrCode())
            {
                mc.getEntities().put(TPConstant.SMC_LOGIN_STATUS, loginRes.getLoginRequestResult().getErrCode());
                return false;
            }
            // after loginrequest,save smc session edit by taochunlong
            String session = (String)mc.getEntities().get(InfoKeys.SMC_SESSION_ID.toString());
            saveSMCSession(session);
            authRes = smcLoginProcess.smcAuthorize(loginRes.getRandomSequence(), pwd);
        }
        else
        {
            String session = (String)mc.getEntities().get(InfoKeys.SMC_SESSION_ID.toString());
            
            //Pass through
            saveSMCSession(session);
            authRes = smcLoginProcess.smcAuthorize(pwd);
            
        }
        mc.getEntities().put(TPConstant.SMC_LOGIN_STATUS, authRes.getAuthenticateResult().getErrCode());
        
        //findbugs 多余的判空检查 
        if (0 == authRes.getAuthenticateResult().getErrCode())
        {
            return true;
        }
        
        return false;
    }
    
    private void saveSMCSession(String smcSession)
    {
        //To add the check null condition for resolving Reconnecting SMC issue (SMC restart)
        if (null != smcSession)
        {
            setSession(smcSession);
        }
        else
        {
            LOGGER.info("smc session id = null");
        }
    }
    
}
